package pkg;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import CRUD.MemberHome;
import Utility.MessageUtility;
import java.util.Vector;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import xmlMapping.Member;
import xmlMapping.Message;

/**
 *
 * @author labib
 */
public class DisplayAllMsgs extends SimpleTagSupport {

    private int memberId;

    /**
     * Called by the container to invoke this tag. 
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */
    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();

        try {


System.out.println("//// mmmm /// / // / / /"+memberId);



            MemberHome memhome = new MemberHome();

            Member mem = memhome.findById(memberId);
            System.out.println(" memeber to find his message is "+memberId);

            MessageUtility utility = new MessageUtility();

            Vector<Message> allmsg = utility.getMessages(mem.getMemberId());
out.print("<form action=delete.do>");
            out.print("<table>");
            out.print("<tr>");
            out.print("<a href=sendMessage.jsp><input type=button value=new></a>");
            out.print("<input type=submit value=delete>");
            
            out.print("</tr>");

            out.print("<tr>");
 out.print("<td>");
  out.print("</td>");
            out.print("<td><h1>");
            out.print("Sender     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp;                 ");
            out.print("</td></h1>");

            out.print("<td><h1>");
            out.print("title   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp; ");
            out.print("</td></h1>");

            out.print("<td><h1>");
            out.print("Date of Send   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;     &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  &nbsp; &nbsp; &nbsp; ");
            out.print("</td></h1>");
            
            out.print("</tr>");
            
            for (int i = 0; i < allmsg.size(); i++) {
                Message temp = allmsg.elementAt(i);
                out.print("<h2>");
                out.print("<tr>");
                out.print("<td>");
                out.print("<input type=checkbox name=allmsg value="+temp.getMsgId()+">");
                 out.print("</td>"); 
                 out.print("<td>");
                out.print(temp.getMemberBySenderId().getUserName());
                out.print("</td>");

                if(temp.isOpened())
                {
                       out.print("<td><h1>");
                out.print(temp.getTitle());
                out.print("</h1></td>");
                }
                else
                {
                    out.print("<td><h2>");
                out.print(temp.getTitle());
                out.print("</h2></td>");   
                }                
             

                out.print("<td>");
                out.print(temp.getDate());
                out.print("</td>");

                out.print("<td>");
                out.print("<h1><a href=dispMsg.jsp?msgid=" + temp.getMsgId() + ">" + "view" + "</a></h1>");
                out.print("</td>");

                out.print("</tr>");
out.print("</h2>");
            }
            out.print("</table>");
out.print("</form>");



            JspFragment f = getJspBody();
            if (f != null) {
                f.invoke(out);
            }

            // TODO: insert code to write html after writing the body content.
            // e.g.:
            //
            // out.println("    </blockquote>");

        } catch (java.io.IOException ex) {
            throw new JspException("Error in DisplayAllMsgs tag", ex);
        }
    }

    public void setMemberId(int memberId) {
        this.memberId = memberId;
    }

    public int getMemberId() {
        return memberId;
    }
    
}
